package com.example.sharesinfo.fragment.search;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sharesinfo.R;
import com.example.sharesinfo.adapter.CommonAdapter;
import com.example.sharesinfo.adapter.CommonViewHolder;
import com.example.sharesinfo.base.BaseFragment;
import com.example.sharesinfo.manager.NewsManager;
import com.example.sharesinfo.model.SearchNewsModel;
import com.example.sharesinfo.model.SearchStockModel;
import com.example.sharesinfo.model.SearchUsersModel;
import com.example.sharesinfo.utils.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


public class SearchInformationFragment extends BaseFragment {
    /**
     * 新闻统一跳转做一下
     */
    private static final int GetStartSearchNewsSuccess = 1001;
    private static final int GetStartSearchNewsFailed = 1002;
    private TextView hot_news_1;
    private TextView hot_news_2;
    private TextView hot_news_3;
    //搜索结果
    public LinearLayout start;
    public TextView title;
    public RecyclerView mInfoView;
    public CommonAdapter<SearchNewsModel> mInfoAdapter;
    public List<SearchNewsModel> mList = new ArrayList<>();
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case GetStartSearchNewsSuccess:
                    JSONArray array = (JSONArray) msg.obj;
                    try {
                        JSONObject object1 = array.getJSONObject(0);
                        JSONObject object2 = array.getJSONObject(1);
                        JSONObject object3 = array.getJSONObject(2);
                        hot_news_1.setText(object1.getString("Title"));
                        hot_news_2.setText(object2.getString("Title"));
                        hot_news_3.setText(object3.getString("Title"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case GetStartSearchNewsFailed:
                    Toast.makeText(getContext(), "获取数据失败", Toast.LENGTH_SHORT).show();
                    break;
            }
            return false;
        }
    });

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_information, null);
        initView(view);
        getStartSearchNews();
        return view;
    }

    private void initView(View view) {
        start = view.findViewById(R.id.start);
        title = view.findViewById(R.id.title);
        hot_news_1 = view.findViewById(R.id.hot_news_1);
        hot_news_2 = view.findViewById(R.id.hot_news_2);
        hot_news_3 = view.findViewById(R.id.hot_news_3);
        mInfoView = view.findViewById(R.id.information_search);
        mInfoView.setLayoutManager(new LinearLayoutManager(getContext()));
        mInfoView.addItemDecoration(new DividerItemDecoration(getContext(),
                DividerItemDecoration.VERTICAL));
        mInfoAdapter = new CommonAdapter<>(mList, new CommonAdapter.OnBindDataListener<SearchNewsModel>() {
            @Override
            public void onBindViewHolder(final SearchNewsModel model, CommonViewHolder viewHolder, int type, int position) {
                viewHolder.setText(R.id.tv_title, model.getTitle());
                viewHolder.setText(R.id.tv_content, model.getContent());
                viewHolder.setText(R.id.tv_source, model.getSource());
                viewHolder.setText(R.id.tv_time, model.getIssueTime());
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        NewsManager.getInstance().queryNews(Integer.parseInt(model.getId()), getContext());
                    }
                });
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.layout_search_info_item;
            }
        });
        mInfoView.setAdapter(mInfoAdapter);
    }

    private void getStartSearchNews() {
        String url = "http://119.23.221.142/api/information/GetStartSearchNews";
        OkHttpUtils.getInstance().Get(url, new OkHttpUtils.okHttpListener() {

            @Override
            public void OnSuccess(String s, int code) throws JSONException {
                Message message = mHandler.obtainMessage();
                JSONObject object = new JSONObject(s);
                if (object.getString("code").equals("200")) {
                    message.what = GetStartSearchNewsSuccess;
                    message.obj = object.getJSONArray("data");
                    mHandler.sendMessage(message);
                } else {
                    message.what = GetStartSearchNewsFailed;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void OnFailed(String s) {

            }
        });
    }

}
